import urllib
from urllib.request import quote
from bs4 import BeautifulSoup
import excel
minit_url = "http://www.boohee.com"
minit_search_url = "http://www.boohee.com/food/search?keyword="

# 爬取薄荷网数据
def minit_search_by_word(str_array):
    urls_src ={}
    urls = []
    for str in str_array:
        # 拼接薄荷搜索 URL 链接
        url = minit_search_url + quote(str)
        response = urllib.request.urlopen(url)
        html = response.read().decode('utf-8')
        soup = BeautifulSoup(html, 'html.parser')
        href = soup.find(name='div', class_='widget-food-list pull-left').find(name='div', class_='img-box pull-left').find(name='a').get('href')
        urls_src[minit_url+href] = soup.find(name='div', class_='widget-food-list pull-left').find(name='div', class_='img-box pull-left').find(name='img').get('src')
        urls.append(minit_url+href)
        print(minit_url+href)
        # print(urls[minit_url+href])
    return urls
def minit_get_data(url,str):
    content =[]
    response = urllib.request.urlopen(url)
    html = response.read().decode('utf-8')
    soup = BeautifulSoup(html, 'html.parser')
    try:
        kind = soup.find(name='ul', class_='basic-infor').find(name='a').string
    except:
        return
    print(soup.find_all(name='span', class_='dd')[2].string)
    print(soup.find_all(name='span', class_='dd')[3].string)
    print(soup.find_all(name='span', class_='dd')[4].string)
    print(soup.find_all(name='span', class_='dd')[5].string)
    calorie = soup.find_all(name='span', class_='dd')[2].string
    sugar = soup.find_all(name='span', class_='dd')[3].string
    fat = soup.find_all(name='span', class_='dd')[4].string
    protain = soup.find_all(name='span', class_='dd')[5].string
    content.append(kind)
    content.append(str)
    content.append(calorie)
    content.append(fat)
    content.append(sugar)
    content.append(protain)
    content.append(url)
    return content


def down_load_pic(url):
    response = urllib.request.urlopen(url)
    html = response.read().decode('utf-8')
    soup = BeautifulSoup(html, 'html.parser')
    img = soup.find(name='div', class_='widget-food-list pull-left')\
        .find(name='div', class_='food-pic pull-left')\
        .find(name='a')\
        .find(name='img')\
        .get('src')
    print(img)


if __name__ == '__main__':
    down_load_pic("http://www.boohee.com/shiwu/mantou_junzhi")
